//
//  YbscVC.swift
//  Auscultation
//
//  Created by 来 on 2021/1/20.
//

import UIKit

class YbscVC: BaseVC {
    
    var data: [String: Any]!
    
    var scrollView: UIScrollView!
    
    var xm: LGFLoginImageTitleView!
    var xb: LGFLoginImageTitleView!
    var csrq: LGFLoginImageTitleView!
    var sg: LGFLoginImageTitleView!
    var tz: LGFLoginImageTitleView!
    var dq: LGFLoginImageTitleView!
    var xxdz: LGFLoginImageTitleView!
    var sjh: LGFLoginImageTitleView!
    var sfz: LGFLoginImageTitleView!

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        getData()
        NotificationCenter.default.addObserver(self, selector: #selector(YbscVC.BdhzVCNoti(_:)), name: NSNotification.Name(rawValue: "BdhzVCNoti"), object: nil)
    }
    
    override func rightBtnSelect(_ sender: UIButton) {
        super.rightBtnSelect(sender)
        let vc = BdhzVC()
        vc.data = self.data
        self.navigationController?.show(vc, sender: self)
    }

    func getData() {
        let id = ((data["patient"] as! [String: Any])["id"] as? String) ?? ""
        if id.count == 0 {
            return
        }
        let paramet: [String: String] = [
            "encryptedData": "zFoamlSX95gBI7S05+e6HakuGltzhbckTNdkY0yZ2hU="
        ]
        EWERequest.lgf("patient/" + id, paramet, .get) { [weak self] (data) in
            self?.xm.text = (data["name"] as? String) ?? ""
            self?.xb.text = ((data["gender"] as? Int) ?? 0) == 1 ? "男" : "女"
            self?.xb.lgf_Name = ((data["gender"] as? Int) ?? 0).description
            self?.csrq.text = ((data["birthday"] as? Int) ?? 0).description.lgf_TimeStampToDate().lgf_TimeStr("yyyy-MM-dd")
            self?.csrq.lgf_Name = ((data["birthday"] as? Int) ?? 0).description
            self?.sg.text = (data["height"] as? String) ?? (self?.data["height"] as? String) ?? ""
            self?.tz.text = (data["weight"] as? String) ?? (self?.data["weight"] as? String) ?? ""
            self?.sjh.text = (data["phone"] as? String) ?? ""
            self?.sfz.text = (data["idCard"] as? String) ?? ""
            self?.dq.text = (data["address"] as? String) ?? ""
            self?.xxdz.text = (data["detailAddress"] as? String) ?? ""
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }
    
    @objc func s_sc(_ sender: UIButton) {
        let file = LGF_Documents_WAV + ((data["wav_path"] as? String) ?? "")
        let id = ((data["patient"] as! [String: Any])["id"] as? String) ?? ""
        let height = (data["height"] as? String) ?? ""
        let weight = (data["weight"] as? String) ?? ""
        let position = (data["position"] as? String) ?? ""
        let calm = (data["calm"] as? String) ?? ""
        let coordinate = (data["coordinate"] as? String) ?? ""
        let site = (data["site"] as? String) ?? ""
        let operation = (data["operation"] as? String) ?? ""
        let collectionAddress = (data["collectionAddress"] as? String) ?? ""
        EWERequest.uploadFile([file], "30", "wav") { (path) in
            if path.count == 0 {
                return
            }
            let paramet: [String: Any] = [
                "hospitalId": EWEUser.lgf.hospitalId ?? "",
                "patientId": id,
                "screeningDetail": ["wavPath": path],
                "patient": ["id": id],
                "height": height,
                "weight": weight,
                "position": position,
                "calm": calm,
                "coordinate": coordinate,
                "site": site,
                "operation": operation,
                "collectionAddress": collectionAddress,
            ]
            EWERequest.lgf("screening", paramet, .post, JSONEncoding.default) { [weak self] (data) in
                SVProgressHUD.showSuccess("上传成功")
                Timer.lgf_After(S: 1.0) {
                    var rdataArray: [Any] = (UserDefaults.standard.array(forKey: LGF_BD_WAV_ARRAY) ?? [])
                    for (i, v) in rdataArray.enumerated() {
                        if let data2 = v as? [String: Any] {
                            if let name = data2["name"] as? String {
                                if let name2 = self?.data["name"] as? String {
                                    if name2 == name {
                                        self!.data["type"] = "已上传"
                                        rdataArray[i] = self!.data as Any
                                        UserDefaults.standard.setValue(rdataArray, forKey: LGF_BD_WAV_ARRAY)
                                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "BdhzVCNoti"), object: self?.data)
                                        self?.navigationController?.popViewController(animated: true)
                                        return
                                    }
                                }
                            }
                        }
                    }
                }
            } _: { (error) in
                SVProgressHUD.showError(error)
            }
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }
    
}

extension YbscVC {
    @objc func BdhzVCNoti(_ sender: Notification) {
        if let data = sender.object as? [String: Any] {
            self.data = data
            getData()
        }
    }
}
